/*
 * Copyright 2017 Zhihu Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhihu.matisse.library.component;

import com.zhihu.matisse.library.ResourceTable;
import com.zhihu.matisse.library.utils.AttrUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.PixelMap;

import java.io.IOException;

public class CheckRadioView extends Image {

    private int mSelectedColor;
    private int mUnSelectUdColor;

    public CheckRadioView(Context context) {
        super(context);
        init();
    }



    public CheckRadioView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        try {
            mSelectedColor = getContext().getResourceManager().getElement(ResourceTable.Color_zhihu_item_checkCircle_backgroundColor).getColor();
            mUnSelectUdColor = getContext().getResourceManager().getElement(ResourceTable.Color_zhihu_check_original_radio_disable).getColor();
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }

        setChecked(false);
    }

    public void setChecked(boolean enable) {
        if (enable) {
            PixelMap element =  AttrUtils.getPixelMap(mContext,ResourceTable.Media_ic_preview_radio_on);
            Canvas canvas = new Canvas(new Texture(element));
            canvas.drawColor (mSelectedColor, Canvas.PorterDuffMode.SRC_IN);
            setImageElement(new PixelMapElement(element));

        } else {
            PixelMap element =  AttrUtils.getPixelMap(mContext,ResourceTable.Media_ic_preview_radio_off);
            Canvas canvas = new Canvas(new Texture(element));
            canvas.drawColor (mUnSelectUdColor, Canvas.PorterDuffMode.SRC_IN);
            setImageElement(new PixelMapElement(element));
        }
    }


    public void setColor(Color color) {
        PixelMap pixelMap = getPixelMap();
        Canvas canvas = new Canvas(new Texture(pixelMap));
        canvas.drawColor (color.getValue(), Canvas.PorterDuffMode.SRC_IN);
        setImageElement(new PixelMapElement(pixelMap));
    }
}
